package com.receive;

import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;

import com.database.LoginDb;
import com.design.Layers;
import com.design.LoginDesign;
import com.design.ClientForm;
import com.request.Details;
import com.request.ServerResponseVO;

public class ClientRecv extends Thread 
{
	String[] clientInfo;
	Details details=null;
	int port;
	String system;
	ServerSocket serverSocket=null;
	Socket socket=null;
	ObjectInputStream objectInputStream=null;
	public static String  accountNUmber="";
public ClientRecv() 
{
	details = new Details();
	clientInfo = details.Config("clientport", "clientsystem");
	port = Integer.parseInt(clientInfo[0]);
	system = clientInfo[1];
	start();
}
public void run()
{
	try
	{
		serverSocket=new ServerSocket(port) ;	
		while(true)
		{
			socket=serverSocket.accept();
			objectInputStream=new ObjectInputStream(socket.getInputStream());
			Object object=objectInputStream.readObject();
			Thread.sleep(1000);
			if(object instanceof ServerResponseVO)
			{
				String result="";
				String error="";
				
				ServerResponseVO serverResponseVO=(ServerResponseVO)object;
				result=serverResponseVO.getResult();
				error=serverResponseVO.getError();
				System.out.println("result................."+result);
				if(result==null)
				{
					Layers.jTextAreaRequest.setText("");
					JOptionPane.showMessageDialog(null, serverResponseVO.getError());
						
				}
				else if(result.contains("success"))
				{
					String arr[]=result.split(" ");
					accountNUmber=arr[0];
					JOptionPane.showMessageDialog(null, "Successfully logged In..");
					new ClientForm();
					LoginDesign.mainform.setVisible(false);
				}
				else if(result.length()>0&&error.length()>0)
				{
					LoginDesign.mainform.setVisible(false);
					JOptionPane.showMessageDialog(null, serverResponseVO.getError());
					
				}
				else if(result.equalsIgnoreCase("registered"))
				{
					JOptionPane.showMessageDialog(null, result);
				}
				else if(!result.equals(""))
				{
					JOptionPane.showMessageDialog(null, result);
				}
				else
				{
					Layers.jTextAreaRequest.setText(result);	
				}
				result="";
				
			}
		}
	}
	catch (Exception e) 
	{
		e.printStackTrace();
	}
	
}
public static void main(String[] args) {
	new ClientRecv();
}
}
